<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller;

use App\Logic\MatchLogic;
use App\Middleware\AuthMiddleware;
use App\Service\SettingService;
use Exception;
use Hyperf\Context\Context;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;
use Psr\Http\Message\ResponseInterface;

#[AutoController]
class MatchController extends AbstractController
{
    /**
     * 获取比赛列表.
     */
    public function matchList(): ResponseInterface
    {
        $this->parseToken();
        $userid = Context::get('userid', 0);
        $input = $this->request->all();
        $data = MatchLogic::getMatchList($userid, $input);
        return $this->apiReturn(200, 'succ', $data);
    }

    /**
     * 获取比赛详情.
     */
    public function matchInfo(): ResponseInterface
    {
        $this->parseToken();
        $userid = Context::get('userid', 0);
        $matchId = $this->request->input('matchId');
        $roomId = $this->request->input('roomId', 0);
        if (empty($matchId)) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            $info = MatchLogic::getMatchInfo($userid, $matchId, $roomId);
            return $this->apiReturn(200, 'succ', $info);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 获取直播比赛的其他主播列表.
     */
    public function matchAnchor(): ResponseInterface
    {
        $matchId = $this->request->input('matchId');
        if (empty($matchId)) {
            return $this->apiReturn(400, '参数错误');
        }
        self::parseToken();
        $userid = Context::get('userid', 0);
        $list = MatchLogic::getRelationAnchor($matchId, $userid);
        return $this->apiReturn(200, 'succ', $list);
    }

    /**
     * 获取比赛指数.
     */
    public function matchOdds(): ResponseInterface
    {
        $matchId = $this->request->input('matchId');
        if (empty($matchId)) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            $data = MatchLogic::getOdds($matchId);
            return $this->apiReturn(200, 'succ', $data);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 获取比赛趋势
     * @return ResponseInterface
     */
    public function matchTrend(): ResponseInterface
    {
        $matchId = $this->request->input('matchId');
        if (empty($matchId)) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            $data = MatchLogic::getTrend($matchId);
            return $this->apiReturn(200, 'succ', $data);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 获取比赛重要事件
     * @return ResponseInterface
     */
    public function matchIncidents(): ResponseInterface
    {
        $matchId = $this->request->input('matchId');
        if (empty($matchId)) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            $data = MatchLogic::getIncidents($matchId);
            return $this->apiReturn(200, 'succ', $data);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 获取直播走势详情.
     */
    public function matchOddsDetail()
    {
    }

    /**
     * 获取比赛数据.
     */
    public function matchStats(): ResponseInterface
    {
        $matchId = $this->request->input('matchId');
        if (empty($matchId)) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            $data = MatchLogic::getStats($matchId);
            return $this->apiReturn(200, 'succ', $data);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 获取比赛阵容
     * @return ResponseInterface
     */
    public function matchLineup(): ResponseInterface
    {
        $matchId = $this->request->input('matchId');
        if (empty($matchId)) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            $data = MatchLogic::getLineup($matchId);
            return $this->apiReturn(200, 'succ', $data);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 获取比赛球队统计
     * @return ResponseInterface
     */
    public function matchTeamStats(): ResponseInterface
    {
        $matchId = $this->request->input('matchId');
        if (empty($matchId)) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            $data = MatchLogic::getTeamStats($matchId);
            return $this->apiReturn(200, 'succ', $data);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 获取比赛分析
     * @return ResponseInterface
     */
    public function matchAnalysis(): ResponseInterface
    {
        $matchId = $this->request->input('matchId');
        if (empty($matchId)) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            $data = MatchLogic::getAnalysis($matchId);
            return $this->apiReturn(200, 'succ', $data);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 获取比赛tables
     * @return ResponseInterface
     */
    public function matchTables(): ResponseInterface
    {
        $matchId = $this->request->input('matchId');
        if (empty($matchId)) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            $data = MatchLogic::getTables($matchId);
            return $this->apiReturn(200, 'succ', $data);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 获取比赛情报
     * @return ResponseInterface
     */
    public function matchNews(): ResponseInterface
    {
        $matchId = $this->request->input('matchId');
        if (empty($matchId)) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            $data = MatchLogic::getNews($matchId);
            return $this->apiReturn(200, 'succ', $data);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 获取文字直播.
     */
    public function matchTextLives(): ResponseInterface
    {
        $matchId = $this->request->input('matchId');
        if (empty($matchId)) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            $data = MatchLogic::getTextLives($matchId);
            return $this->apiReturn(200, 'succ', $data);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 获取球员统计
     * @return ResponseInterface
     */
    public function matchPlayerStats(): ResponseInterface
    {
        $matchId = $this->request->input('matchId');
        $playerId = $this->request->input('playerId');
        if (empty($matchId) || empty($playerId)) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            $data = MatchLogic::getMatchPlayerStats($matchId, $playerId);
            return $this->apiReturn(200, 'succ', $data);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 获取比赛投票.
     */
    public function getVote()
    {
    }

    /**
     * 关注比赛
     * @return ResponseInterface
     */
    #[Middleware(AuthMiddleware::class)]
    public function attention(): ResponseInterface
    {
        $userid = Context::get('userid', 0);
        $matchId = $this->request->input('matchId', '');
        if (empty($matchId)) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            $isColl = MatchLogic::collectMatch($userid, $matchId);
            return $this->apiReturn(200, '操作成功', ['isColl' => $isColl]);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 获取预约比赛
     * @return ResponseInterface
     */
    #[Middleware(AuthMiddleware::class)]
    public function myAttentionMatchList(): ResponseInterface
    {
        $userId = Context::get('userid', 0);
        $data = MatchLogic::myAttentionMatchList($userId);
        return $this->apiReturn(200, 'succ', $data);
    }

    /**
     * 热门搜索关键词
     * @return ResponseInterface
     */
    public function hotSearch(): ResponseInterface
    {
        $getHotWords = function($sportId) {
            $arr = array_filter(explode("\n", SettingService::get('search_hot_words_'.$sportId, '')));
            array_walk($arr, function (&$v) {
               $v = trim($v);
            });
            return $arr;
        };
        $hotWords = [
            1 => $getHotWords(1),
            2 => $getHotWords(2),
            3 => $getHotWords(3),
            10 => $getHotWords(10),
        ];
        return $this->apiReturn(200, 'succ', $hotWords);
    }

    /**
     * 比赛搜索
     * @return ResponseInterface
     */
    public function search(): ResponseInterface
    {
        $this->parseToken();
        $userId = Context::get('userid', 0);
        $input = $this->request->all();
        if (empty($input['query']['keyword'])) {
            return $this->apiReturn(400, '搜索关键词不能空');
        }
        if (empty($input['query']['sportId'])) {
            return $this->apiReturn(400, '搜索类型选择错误');
        }
        $data = MatchLogic::getMatchList($userId, $input);
        return $this->apiReturn(200, 'succ', $data);
    }
}
